import java.awt.Color;
import java.awt.Graphics;
import java.util.Map;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.events.ServerMessageEvent;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.event.listeners.ServerMessageListener;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class DummyGotCursedNoAntiban extends Script implements
		ServerMessageListener, PaintListener {
	long lastanti = System.currentTimeMillis();
	long curranti;
	long anticheck;
	public boolean norunes = false;
	public int spell = 0;
	public String spelln = "";
	public int runecount = 0;
	public int startlvl = 0;
	public int startxp = 0;
	public int style = 0;
	public int castxp = 0;
	Color BG = new Color(0, 0, 0, 100);
	public long startTime = System.currentTimeMillis();

	public int antiBan() {
		random(1, 200);
		return random(100, 200);
	}

	@Override
	public String getAuthor() {
		return "Dharok";
	}

	@Override
	public String getName() {
		return "DummyGotCursedNoAntiban";
	}

	@Override
	public String getScriptCategory() {
		return "Magic";
	}

	@Override
	public String getScriptDescription() {
		return "<html>\n"
				+ "<head></head>\n"
				+ "<body>\n"
				+ "<center>"
				+ "<h2>"
				+ getName()
				+ "</h2>"
				+ "</center>\n"
				+ "<b>Author:</b> "
				+ getAuthor()
				+ "<br/>\n"
				+ "Confuse:<input type=\"radio\" name=\"style\" value=\"confuse\"> "
				+ "Curse:<input type=\"radio\" name=\"style\" value=\"curse\" checked=\"checked\"> "
				+ "</body>\n" + "</html>";
	}

	@Override
	public double getVersion() {
		return 0.1;
	}

	public boolean isInArea(final int maxX, final int minY, final int minX,
			final int maxY) {
		final int x = getMyPlayer().getLocation().getX();
		final int y = getMyPlayer().getLocation().getY();
		if (x >= minX && x <= maxX && y >= minY && y <= maxY) {
			return true;
		}
		return false;
	}

	@Override
	public int loop() {
		try {
			if (getMyPlayer().isMoving()) {
				return random(300, 400);
			}
			if (getMyPlayer().isInCombat() && getPlane() == 0) {
				if (atTile(new RSTile(3212, 3256), "Climb-up")) {
					return random(30000, 40000);
				}
				return random(300, 400);
			}
			if (getPlane() == 1) {
				atTile(new RSTile(3213, 3256), "Climb-down");
				return random(500, 600);
			}
			if (!isInArea(3213, 3252, 3210, 3254)) {
				walkTileMM(new RSTile((3213 - random(0, 3)), (3254 - random(0,
						2))));
				return random(600, 700);
			}
			if (norunes) {
				return -1;
			}
			final RSNPC dummy = getNearestFreeNPCByID(4474);
			if (!Bot.getClient().isSpellSelected()) {
				if (getCurrentTab() != 6) {
					openTab(6);
				}
				castSpell(spell);
				return antiBan();
			}
			if (dummy != null && getMyPlayer().getAnimation() == -1) {
				atNPC(dummy, "Cast " + spelln + " -> Magic dummy");
			}
			antiBan();
		} catch (final Exception e) {
			e.printStackTrace();
		}
		return antiBan();
	}

	@Override
	public void onFinish() {
		Bot.getEventManager().removeListener(ServerMessageListener.class, this);
		Bot.getEventManager().removeListener(PaintListener.class, this);
	}

	public void onRepaint(final Graphics g) {
		if (isLoggedIn()) {
			long millis = System.currentTimeMillis() - startTime;
			final long hours = millis / (1000 * 60 * 60);
			millis -= hours * 1000 * 60 * 60;
			final long minutes = millis / (1000 * 60);
			millis -= minutes * 1000 * 60;
			final long seconds = millis / 1000;
			final int LevelChange = skills
					.getCurrentSkillLevel(Constants.STAT_MAGIC)
					- startlvl;
			final int XPChange = skills
					.getCurrentSkillExp(Constants.STAT_MAGIC)
					- startxp;
			float XPperSec = 0;
			if ((minutes > 0 || hours > 0 || seconds > 0) && XPChange > 0) {
				XPperSec = (float) XPChange
						/ (float) (seconds + minutes * 60 + hours * 60 * 60);
			}

			final float XPperMin = XPperSec * 60;
			final float XPperHour = XPperMin * 60;
			final float timeTillLvl = 9999;
			float secsTillLvl = (int) timeTillLvl;
			float minsTillLvl = (int) timeTillLvl;
			float hoursTillLvl = (int) timeTillLvl;

			if (XPperSec > 0) {
				secsTillLvl = skills.getXPToNextLevel(Constants.STAT_MAGIC)
						/ XPperSec;
			}
			if (XPperMin > 0) {
				minsTillLvl = skills.getXPToNextLevel(Constants.STAT_MAGIC)
						/ XPperMin;
			}
			if (XPperHour > 0) {
				hoursTillLvl = skills.getXPToNextLevel(Constants.STAT_MAGIC)
						/ XPperHour;
			}
			secsTillLvl -= (int) minsTillLvl * 60;
			minsTillLvl -= (int) hoursTillLvl * 60;

			if (minsTillLvl < 0) {
				minsTillLvl = 0;
			}
			g.setColor(BG);
			g.fill3DRect(332, 258, 184, 75, true);
			g.setColor(Color.white);
			g.drawString("Runtime: " + hours + "h " + minutes + "m " + seconds
					+ "s.", 335, 270);
			g.drawString("Magic levels gained: " + LevelChange, 335, 282);
			g
					.drawString(
							"Magic EXP gained: "
									+ (skills
											.getCurrentSkillExp(Constants.STAT_MAGIC) - startxp),
							335, 294);
			g.drawString("EXP per hour: " + (int) XPperHour, 335, 306);
			g.drawString("Casts per hour: " + (int) XPperHour / castxp, 335,
					318);
			g.drawString("Time to level: " + (int) hoursTillLvl + "h "
					+ (int) minsTillLvl + "m " + (int) secsTillLvl + "s", 335,
					330);
		}
	}

	@Override
	public boolean onStart(final Map<String, String> args) {
		if (args.get("style").equals("curse")) {
			spell = 12;
			spelln = "Curse";
			castxp = 29;
		}
		startlvl = skills.getCurrentSkillLevel(Constants.STAT_MAGIC);
		startTime = System.currentTimeMillis();
		startxp = skills.getCurrentSkillExp(Constants.STAT_MAGIC);
		runecount = getEquipmentCount(559);
		norunes = false;
		return true;
	}

	public void serverMessageRecieved(final ServerMessageEvent e) {
		if (e.getMessage().contains("You do not have enough")) {
			norunes = false;
		}
	}
}